# coding=utf-8
from django.contrib import admin

from apps.order.models import PriceUpdate
from apps.order.models import BaseOrder


class PriceUpdateAdmin(admin.ModelAdmin):
    list_display = ("id", 'sum_price', 'reserved', 'product_name', "channel_name", "price", "leader_price", "leader_leader_price", "price_type")

    list_per_page = 50

    list_filter = ("price_type",)

    def product_name(self, model):
        return model.channel_product.product.name

    def channel_name(self, model):
        return model.channel_product.channel.name

    product_name.short_description = "产品名称"
    channel_name.short_description = "渠道名称"


# class TeamInComeInline(admin.TabularInline):
#     model = TeamInCome
#     extra = 2  # 用于添加外键关系模型的空行
#     # verbose_name = '类别属性'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
#     # show_change_link = True  # 显示修改连接,这个很有用啊!!!!
#     can_delete = True  # 是否显示删除框


class BaseOrderAdmin(admin.ModelAdmin):
    # inlines = [TeamInComeInline]

    list_display = (
        'product_name', "salesman_name", "buy_user_name", "income", "is_close", "create_time")

    list_filter = ("create_time",)

    list_per_page = 50

    def product_name(self, model):
        return model.product.name

    def salesman_name(self, model):
        # 如果是直推的话，那么申请人和被推荐人是同一个人
        # 如果是代办的话，那么申请人和被推荐人不是同一个人
        tel1 = model.single.b_user.tel
        tel2 = model.single.applicant.tel  # 申请人手机号
        if tel1 == tel2:
            return model.salesman.name + ":直推 " + model.single.b_user.name + " %s" % model.single.id
        else:
            return model.salesman.name + ":代办 " + model.single.applicant.name + " %s" % model.single.id

    def buy_user_name(self, model):
        return model.single.applicant.name

    product_name.short_description = "产品名称"
    salesman_name.short_description = "直推人/代办人"
    buy_user_name.short_description = "购买人/申请人列表中的人"


class TeamInComeAdmin(admin.ModelAdmin):
    list_display = ("salesman_name", "level", "income", "is_close", "create_time")

    def salesman_name(self, model):
        return model.salesman.name

    salesman_name.short_description = "业务员"


admin.site.register(PriceUpdate, PriceUpdateAdmin)
admin.site.register(BaseOrder, BaseOrderAdmin)
# admin.site.register(TeamInCome, TeamInComeAdmin)
